package com.edu.web.controller.info;

import com.edu.common.annotation.Log;
import com.edu.common.core.controller.BaseController;
import com.edu.common.core.domain.AjaxResult;
import com.edu.common.core.page.TableDataInfo;
import com.edu.common.enums.BusinessType;
import com.edu.common.utils.poi.ExcelUtil;
import com.edu.framework.websocket.service.CardService;
import com.edu.system.domain.TCard;
import com.edu.system.service.ITCardService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 卡管理Controller
 *
 * @author xxxx
 * @date 2023-12-17
 */
@RestController
@RequestMapping("/system/card")
public class TCardController extends BaseController {
    @Autowired
    private ITCardService tCardService;
    @Resource
    private CardService wsCardService;

    /**
     * 查询卡管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:card:list')")
    @GetMapping("/list")
    public TableDataInfo list(TCard tCard) {
        startPage();
        List<TCard> list = tCardService.selectTCardList(tCard);
        return getDataTable(list);
    }

    /**
     * 导出卡管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:card:export')")
    @Log(title = "卡管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCard tCard) {
        List<TCard> list = tCardService.selectTCardList(tCard);
        ExcelUtil<TCard> util = new ExcelUtil<TCard>(TCard.class);
        util.exportExcel(response, list, "卡管理数据");
    }

    @Log(title = "批量导入卡", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:card:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<TCard> util = new ExcelUtil<TCard>(TCard.class);
        List<TCard> list = util.importExcel(file.getInputStream());
        String message = tCardService.importCard(list, 0);

        // 批量充值就全量推
        CompletableFuture.runAsync(() -> wsCardService.pushCardList(new ArrayList<>()));
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<TCard> util = new ExcelUtil<TCard>(TCard.class);
        util.importTemplateExcel(response, "卡数据");
    }


    /**
     * 获取卡管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:card:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tCardService.selectTCardById(id));
    }

    /**
     * 挂失/解挂
     */
    @PreAuthorize("@ss.hasPermi('system:card:edit')")
    @GetMapping(value = "/lossOrReturn/{id}/{status}")
    public AjaxResult lossOrReturn(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        TCard card = new TCard();
        card.setId(id);
        card.setStatus(status);
        int i = tCardService.updateTCard(card);

        wsCardService.pushCardList(Collections.singletonList(id));
        return success(i);
    }

    /**
     * 新增卡管理
     */
    @PreAuthorize("@ss.hasPermi('system:card:add')")
    @Log(title = "卡管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCard tCard) {
        if (tCardService.findCardByNo(tCard.getCardNo()) != null){
            return error("该用户的卡已存在");
        }
        int i = tCardService.insertTCard(tCard);
        wsCardService.pushCardList(Collections.singletonList(tCard.getId()));
        return toAjax(i);
    }

    /**
     * 补卡
     */
    @PreAuthorize("@ss.hasPermi('system:card:add')")
    @Log(title = "卡管理", businessType = BusinessType.INSERT)
    @PostMapping("/replacement")
    public AjaxResult replacement(@RequestBody TCard tCard) {
        if (tCardService.findCardByNo(tCard.getCardNo()) != null){
            return error("卡已存在");
        }
        Long newId = tCardService.replaceCard(tCard.getCardNo(), tCard.getId());
        wsCardService.pushCardList(Arrays.asList(tCard.getId(), newId));

        return toAjax(1);
    }

    /**
     * 修改卡管理
     */
    @PreAuthorize("@ss.hasPermi('system:card:edit')")
    @Log(title = "卡管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TCard tCard) {
        return toAjax(tCardService.updateTCard(tCard));
    }

    /**
     * 删除卡管理
     */
    @PreAuthorize("@ss.hasPermi('system:card:remove')")
    @Log(title = "卡管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tCardService.deleteTCardByIds(ids));
    }
}
